package com.movie.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
@Schema(description = "分页响应对象")
public class PageResponse<T> {
    
    @Schema(description = "数据列表")
    private List<T> content;
    
    @Schema(description = "当前页码（从0开始）")
    private int page;
    
    @Schema(description = "每页大小")
    private int size;
    
    @Schema(description = "总记录数")
    private long totalElements;
    
    @Schema(description = "总页数")
    private int totalPages;
    
    @Schema(description = "是否有上一页")
    private boolean hasPrevious;
    
    @Schema(description = "是否有下一页")
    private boolean hasNext;
    
    public static <T> PageResponse<T> of(Page<T> page) {
        PageResponse<T> response = new PageResponse<>();
        response.content = page.getContent();
        response.page = page.getNumber();
        response.size = page.getSize();
        response.totalElements = page.getTotalElements();
        response.totalPages = page.getTotalPages();
        response.hasPrevious = page.hasPrevious();
        response.hasNext = page.hasNext();
        return response;
    }
    
    public static <T> PageResponse<T> empty() {
        PageResponse<T> response = new PageResponse<>();
        response.content = List.of();
        response.page = 0;
        response.size = 10;
        response.totalElements = 0;
        response.totalPages = 0;
        response.hasPrevious = false;
        response.hasNext = false;
        return response;
    }
}